package com.example.myshopping3.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.myshopping3.mapper.OrderMapper;
import com.example.myshopping3.model.OrderDesc;
import com.example.myshopping3.model.Orders;
import com.example.myshopping3.service.OrderDescService;
import com.example.myshopping3.service.OrderService;
import com.example.myshopping3.util.OrderId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author 路老师
 * @date 2025/7/22
 * 无精神，不编程！无乐趣，不编程！
 * “调试：与bug共舞的艺术。”
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Orders>
implements OrderService {
    @Autowired
    private OrderDescService orderDescService;

    @Override
    @Transactional
    public void addOrder(Orders orders, List<OrderDesc> orderDescList) {

        //设置订单编号
        orders.setOrderId(OrderId.getOrderId());
        this.save(orders);
        //添加订单明细
        //循环设置每个订单明细的订单编号
        for (int i = 0; i < orderDescList.size(); i++) {
            orderDescList.get(i).setOrderId(orders.getOrderId());
        }
        orderDescService.saveBatch(orderDescList);
    }
}
